﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Guids;

namespace GuidGeneratorSilverlight
{
    public partial class MainPage : UserControl
    {
        private Options options = new Options();

        public MainPage()
        {
            InitializeComponent();

            this.DataContext = options;
        }

        private void OnButtonGenerateGuid_Click(object sender, RoutedEventArgs e)
        {
            string guid = GuidGenerator.GenerateGuid(options.GuidFormat, options.UpperCase);

            this.textBoxCurrent.Text = guid;
            this.buttonCopy.IsEnabled = true;

            Clipboard.SetText(guid);
        }

        private void OnButtonGenerateMultipleGuids_Click(object sender, RoutedEventArgs e)
        {
            GuidCountWindow window = new GuidCountWindow(options);
            window.Closed += delegate(object senderObj, EventArgs args)
            {
                if (!window.DialogResult ?? false)
                    return;

                string guids = GuidGenerator.GenerateMultipleGuids(options.Count, options.GuidFormat, options.UpperCase);

                this.textBoxCurrent.Text = guids;
                this.buttonCopy.IsEnabled = true;

                Clipboard.SetText(guids);
            };
            window.Show();
        }

        private void OnButtonCopy_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(this.textBoxCurrent.Text);
        }

        private void OnRadioButtonGuidFormat_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            if (radioButton == null)
                return;

            string tag = radioButton.Tag as string;
            if (tag == null)
                return;

            GuidFormats guidFormat;

            if (!Enum.TryParse<GuidFormats>(tag, out guidFormat))
                return;

            string guid = GuidGenerator.GenerateGuid(guidFormat, options.UpperCase);

            this.textBoxCurrent.Text = guid;
            this.buttonCopy.IsEnabled = true;

            Clipboard.SetText(guid);
        }

        private void OnCheckBoxUpperCase_Checked(object sender, RoutedEventArgs e)
        {
            this.textBoxCurrent.Text = this.textBoxCurrent.Text.ToUpper();
            Clipboard.SetText(this.textBoxCurrent.Text);
        }

        private void OnCheckBoxUpperCase_Unchecked(object sender, RoutedEventArgs e)
        {
            this.textBoxCurrent.Text = this.textBoxCurrent.Text.ToLower();
            Clipboard.SetText(this.textBoxCurrent.Text);
        }
    }
}
